package com.task.web.controller.core;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.common.utils.SystemFunctionUtil;
import com.task.common.utils.uuid.UUID;
import com.task.core.domain.DataImportHeadInfo;
import com.task.core.dto.request.DataOperationRequestDTO;
import com.task.core.dto.request.ExportFileRequestDTO;
import com.task.core.service.CheckService;
import com.task.core.service.CountryService;
import com.task.core.service.DataFileOperationService;
import com.task.core.service.DataOperationService;
import com.task.core.service.SysFileService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author 迪迦.
 * @date 2024/11/11 13:49
 */
@Api("数据管理")
@RestController
@RequestMapping("/dataOperation")
public class DataOperationController extends BaseController {

  @Resource
  private DataOperationService dataOperationService;

  @Resource
  private SysFileService fileService;

  @Resource
  private DataFileOperationService dataFileOperationService;

  @Resource
  private CheckService checkService;

  @GetMapping("/getDataById")
  public AjaxResult dataImport(String dataId, String dataType, int num) {
    return success(dataOperationService.getDataById(dataId, SystemFunctionUtil.getTableNameById(dataId, dataType), dataType, num));
  }

  @Log(title = "数据管理", businessType = BusinessType.UPDATE)
  @PutMapping("/initDataStatus")
  public AjaxResult initDataStatus(String dataId, String dataType) {
    dataOperationService.initDataStatus(dataId, dataType, null);
    return success();
  }


  //@GetMapping("/getDataByPage")
  //public AjaxResult getDataByPage(@Validated DataOperationRequestDTO request) {
  //  return success(dataOperationService.getDataByPage(request));
  //}


  @GetMapping("/getDataByPageOld")
  public AjaxResult getDataByPageOld(@Validated DataOperationRequestDTO request) {
    return success(dataOperationService.getDataByPageOld(request));
  }

  @Log(title = "数据管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteByFileCode/{fileCode}/{datasourceCode}/{dataType}")
  public AjaxResult deleteByFileCode(@PathVariable String fileCode,
      @PathVariable String datasourceCode,
      @PathVariable String dataType) {
    dataOperationService.deleteByFileCode(fileCode, datasourceCode, dataType);
    return success();
  }



  @GetMapping("/getImportFileHeader")
  public AjaxResult getImportFileHeader(String sourceCode) {
    return success(dataOperationService.getImportFileHeader(sourceCode));
  }

  @GetMapping("/exportData")
  public AjaxResult exportData(@Validated DataOperationRequestDTO request) {
    String fileCode = UUID.randomUUID().toString().replace("-", "");
    request.setOperationBy(SecurityUtils.getUserId());
    String userName = SecurityUtils.getUsername();
    new Thread(() -> {
      try {
        dataOperationService.exportData(request, fileCode, userName);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }).start();
    return success(fileCode);
  }

  @GetMapping("/getExportFiles")
  public AjaxResult getExportFiles(ExportFileRequestDTO request) {
    return success(dataOperationService.getExportFiles(request));
  }

  @PostMapping("/download/{code}")
  public void download(@PathVariable String code, HttpServletResponse response) {
    dataOperationService.updateDownloadStatus(code, true);
    try {
      fileService.download(code, response);
    }catch (Exception e) {
      dataOperationService.updateDownloadStatus(code, false);
      e.printStackTrace();
    }
  }

  @GetMapping("/getImportHistory")
  public AjaxResult getImportHistory(DataOperationRequestDTO request) {
    return success(dataOperationService.getImportHistory(request));
  }

  @GetMapping("/getDataExportProgress")
  public AjaxResult getDataExportProgress() {
    return success(dataFileOperationService.getDataExportProgress());
  }

  @PostMapping("/clearExportSuccessRecord")
  public AjaxResult clearSuccessRecord(@RequestBody List<String> fileCodes) {
    dataOperationService.clearSuccessRecord(fileCodes);
    return success();
  }

  //@GetMapping("/getDataById/{dataId}")
  //public AjaxResult getDataById(@PathVariable String dataId) {
  //  return success(dataOperationService.getDataById(dataId));
  //}


}
